#include "scrollskin.h"
#include <allegro5/allegro_image.h>
#include <iostream>
#include <cstdlib>
#include "util.h"
using namespace std;

ScrollSkin::ScrollSkin(const string &imagefile, int thickness)
: m_thickness(thickness), m_pBitmap(NULL), m_pRotBitmap(NULL) {
     m_pBitmap = al_load_png(imagefile.c_str());
     if (!m_pBitmap) {
	  cout << "error loading scroll bar image" << endl;
	  exit(1);
     }
     ALLEGRO_BITMAP *pOldBitmap = al_get_target_bitmap();
     // create 90 degree rotated bitmap
     m_pRotBitmap = al_create_bitmap(al_get_bitmap_height(m_pBitmap), 
				     al_get_bitmap_width(m_pBitmap));
     al_set_target_bitmap(m_pRotBitmap);
     double angle = degreesToRadians(90);
     al_draw_rotated_bitmap(m_pBitmap, 0, m_thickness, 0, 0, angle, 0);
     al_set_target_bitmap(pOldBitmap);
}

ScrollSkin::~ScrollSkin() {
     if (m_pBitmap) {
	  al_destroy_bitmap(m_pBitmap);
	  m_pBitmap = NULL;
     }
     if (m_pRotBitmap) {
	  al_destroy_bitmap(m_pRotBitmap);
	  m_pRotBitmap = NULL;
     }
}

int ScrollSkin::thickness() const {
     return m_thickness;
}

ALLEGRO_BITMAP* ScrollSkin::bitmap() {
     return m_pBitmap;
}

ALLEGRO_BITMAP* ScrollSkin::rotBitmap() {
     return m_pRotBitmap;
}
